package com.zoe.onelink.business.util.ws;

import cn.hutool.core.thread.ThreadUtil;
import com.zoe.onelink.core.async.OnelinkContextTaskDecorator;
import com.zoe.onelink.util.ValidUtil;

import java.util.concurrent.ExecutorService;

/**
 * <p>标题: HTTPS请求帮助类</p>
 * <p>描述: 同步、异步</p>
 * <p>版权: Copyright (c) 2018</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: cxy
 * @date: 2018/6/15
 */
@Deprecated
public class HttpsRequestHelper {

    /**
     * 默认执行写入线程数
     */
    private static final int DEFAULT_WRITE_THREADS = 10;

    /**
     * 线程池
     */
    private static ExecutorService pool = ThreadUtil.newExecutor(DEFAULT_WRITE_THREADS);


    private static OnelinkContextTaskDecorator contextCopyingDecorator = OnelinkContextTaskDecorator.getInstance();

    /**
     * 执行post调用(同步)
     *
     * @param url    url地址
     * @param params 请求体
     * @return String 返回信息
     * @throws Exception 异常信息
     */
    public static String doPostSyn(String url, String params) throws Exception {
        if (ValidUtil.isEmptyOrNull(url)) {
            throw new Exception("api接口地址不能为空");
        }
        return HttpsRequestUtil.post(url, params);
    }

    /**
     * 执行post调用(同步)
     *
     * @param url         url地址
     * @param params      请求体
     * @param readTimeOut 超时时间 单位毫秒
     * @return String 返回信息
     * @throws Exception 异常信息
     */
    public static String doPostSyn(String url, String params, int readTimeOut) throws Exception {
        if (ValidUtil.isEmptyOrNull(url)) {
            throw new Exception("api接口地址不能为空");
        }
        return HttpsRequestUtil.post(url, params, readTimeOut);
    }

    /**
     * 执行get调用(同步)
     *
     * @param url    url地址
     * @param params 请求体
     * @return String 返回信息
     * @throws Exception 异常信息
     */
    public static String doGetSyn(String url, String params) throws Exception {
        if (ValidUtil.isEmptyOrNull(url)) {
            throw new Exception("api接口地址不能为空");
        }
        return HttpsRequestUtil.get(url, params);
    }

    /**
     * 执行get调用(同步)
     *
     * @param url         url地址
     * @param params      请求体
     * @param readTimeOut 超时时间 单位毫秒
     * @return String 返回信息
     * @throws Exception 异常信息
     */
    public static String doGetSyn(String url, String params, int readTimeOut) throws Exception {
        if (ValidUtil.isEmptyOrNull(url)) {
            throw new Exception("api接口地址不能为空");
        }
        return HttpsRequestUtil.get(url, params, readTimeOut);
    }

    /**
     * 执行post调用(异步)
     *
     * @param url         url地址
     * @param params      请求体
     * @param readTimeOut 超时时间 单位毫秒
     * @throws Exception 异常信息
     */
    public static void doPostAsyn(String url, String params, int readTimeOut) throws Exception {
        if (ValidUtil.isEmptyOrNull(url)) {
            throw new Exception("api接口地址不能为空");
        }
        pool.submit(contextCopyingDecorator.decorate(new AsynPostTask(url, params, readTimeOut)));
    }

    /**
     * 执行post调用(异步)
     *
     * @param url    url地址
     * @param params 请求体
     * @throws Exception 异常信息
     */
    public static void doPostAsyn(String url, String params) throws Exception {
        if (ValidUtil.isEmptyOrNull(url)) {
            throw new Exception("api接口地址不能为空");
        }
        pool.submit(contextCopyingDecorator.decorate(new AsynPostTask(url, params)));
    }

    /**
     * 执行get调用(异步)
     *
     * @param url    url地址
     * @param params 请求体
     * @throws Exception 异常信息
     */
    public static void doGetAsyn(String url, String params) throws Exception {
        if (ValidUtil.isEmptyOrNull(url)) {
            throw new Exception("api接口地址不能为空");
        }
        pool.submit(contextCopyingDecorator.decorate(new AsynGetTask(url, params)));
    }

    /**
     * 执行get调用(异步)
     *
     * @param url         url地址
     * @param params      请求体
     * @param readTimeOut 超时时间 单位毫秒
     * @throws Exception 异常信息
     */
    public static void doGetAsyn(String url, String params, int readTimeOut) throws Exception {
        if (ValidUtil.isEmptyOrNull(url)) {
            throw new Exception("api接口地址不能为空");
        }
        pool.submit(contextCopyingDecorator.decorate(new AsynGetTask(url, params, readTimeOut)));
    }

    /**
     * 异步调用任务
     */
    private static class AsynPostTask implements Runnable {
        // api接口地址
        private String url;
        // 请求体
        private String params;
        // 超时时间
        private int readTimeOut = 0;

        AsynPostTask(String url, String params) {
            this.url = url;
            this.params = params;
        }

        AsynPostTask(String url, String params, int readTimeOut) {
            this.url = url;
            this.params = params;
            this.readTimeOut = readTimeOut;
        }

        @Override
        public void run() {
            try {
                if (0 == readTimeOut) {
                    HttpsRequestUtil.post(url, params);
                } else {
                    HttpsRequestUtil.post(url, params, readTimeOut);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 异步调用任务
     */
    private static class AsynGetTask implements Runnable {
        // api接口地址
        private String url;
        // 请求体
        private String params;
        // 超时时间
        private int readTimeOut = 0;

        AsynGetTask(String url, String params) {
            this.url = url;
            this.params = params;
        }

        AsynGetTask(String url, String params, int readTimeOut) {
            this.url = url;
            this.params = params;
            this.readTimeOut = readTimeOut;
        }

        @Override
        public void run() {
            try {
                if (0 == readTimeOut) {
                    HttpsRequestUtil.get(url, params);
                } else {
                    HttpsRequestUtil.get(url, params, readTimeOut);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
